package com.jackaltsc.mydemodatastorage.sqlite;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 这个类实现了接口UserDao 负责访问数据库
 */
public class UserDaoImp implements UserDao {

    private SQLiteHelper sqLiteHelper;

    //构造函数 实例化SQLiteHelper
    public UserDaoImp(Context context) {
        sqLiteHelper = new SQLiteHelper(context);
    }

    @Override
    public boolean userAddOne(User mUser) {

        boolean flag = false;
        SQLiteDatabase sqLiteDatabase = null;

        try {

            String sqlStr = "insert into " + SQLiteHelper.TABLENAME
                    + "(userid,username,userpasswd) values (?,?,?) ";

            sqLiteDatabase = sqLiteHelper.getWritableDatabase();

            int id = mUser.getUserId();
            String name = mUser.getUserName();
            String passwd = mUser.getUserPasswd();

            Object[] params = new Object[3];
            params[0] = id;
            params[1] = name;
            params[2] = passwd;

            sqLiteDatabase.execSQL(sqlStr, params);
            flag = true;

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (sqLiteDatabase != null) {
                sqLiteDatabase.close();
            }
        }

        return flag;
    }

    @Override
    public boolean userDeleteOne(int mUserId) {

        boolean flag = false;
        //SQLite管理类
        SQLiteDatabase sqLiteDatabase = null;

        try {
            String sqlStr = "delete from " + SQLiteHelper.TABLENAME
                    + " where userid = ?";

            //实例化SQLite管理类
            sqLiteDatabase = sqLiteHelper.getWritableDatabase();

            Object[] params = new String[1];
            params[0] = mUserId;

            //执行sql语句 这里是删除
            sqLiteDatabase.execSQL(sqlStr, params);

            flag = true;
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (sqLiteDatabase != null) {
                sqLiteDatabase.close();
            }
        }
        return flag;
    }

    @Override
    public boolean userUpDateOne(User mUser) {

        boolean flag = false;
        SQLiteDatabase sqLiteDatabase = null;

        try {

            String sqlStr = "update " + SQLiteHelper.TABLENAME +
                    " set username = ? , userpasswd = ? where userid = ?";
            sqLiteDatabase = sqLiteHelper.getWritableDatabase();

            int id = mUser.getUserId();
            String name = mUser.getUserName();
            String passwd = mUser.getUserPasswd();

            Object[] params = new Object[3];
            params[0] = id;
            params[1] = name;
            params[2] = passwd;
            sqLiteDatabase.execSQL(sqlStr, params);

            flag = true;

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (sqLiteDatabase != null) {
                sqLiteDatabase.close();
            }
        }

        return flag;
    }

    @Override
    public User userQueryOne(int mUserId) {

        User user = new User();

        SQLiteDatabase sqLiteDatabase = null;

        try {

            String sqlStr = "select * from " + SQLiteHelper.TABLENAME
                    + " where userid = ?";

            sqLiteDatabase = sqLiteHelper.getReadableDatabase();

            String[] params = new String[1];
            params[0] = String.valueOf(mUserId);

            Cursor cursor = sqLiteDatabase.rawQuery(sqlStr, params);

            //得到列数
            int columns = cursor.getColumnCount();
            while (cursor.moveToNext()) {
                for (int i = 0; i < columns; i++) {

                    //列名
                    String columnName = cursor.getColumnName(i);
                    //值
                    String columnValue = cursor.getString(cursor
                            .getColumnIndex(columnName));

                    if (columnValue == null) {
                        columnValue = "";
                    }

                    switch (columnName) {

                        case "userid":
                            user.setUserId(Integer.valueOf(columnValue));
                            break;

                        case "username":
                            user.setUserName(columnValue);
                            break;

                        case "userpasswd":
                            user.setUserPasswd(columnValue);
                            break;
                    }
                }
            }

            cursor.close();

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (sqLiteDatabase != null) {
                sqLiteDatabase.close();
            }
        }

        return user;
    }

    @Override
    public List<User> userQueryMul() {

        List<User> userList = new ArrayList<>();

        SQLiteDatabase sqLiteDatabase = null;

        try {

            String sqlStr = "select * from " + SQLiteHelper.TABLENAME;

            sqLiteDatabase = sqLiteHelper.getReadableDatabase();

            Cursor cursor = sqLiteDatabase.rawQuery(sqlStr, null);

            int columns = cursor.getColumnCount();

            while (cursor.moveToNext()) {

                User user = new User();

                for (int i = 0; i < columns; i++) {

                    String columnName = cursor.getColumnName(i);
                    String columnValue = cursor.getString(cursor
                            .getColumnIndex(columnName));

                    if (columnValue == null) {
                        columnValue = "";
                    }

                    switch (columnName) {

                        case "userid":
                            user.setUserId(Integer.valueOf(columnValue));
                            break;

                        case "username":
                            user.setUserName(columnValue);
                            break;

                        case "userpasswd":
                            user.setUserPasswd(columnValue);
                            break;
                    }

                }

                userList.add(user);
            }

            cursor.close();

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (sqLiteDatabase != null) {
                sqLiteDatabase.close();
            }
        }

        return userList;
    }
}
